package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.Friend;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.MovementTimeLine;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Syntax;
import java.util.List;

@Component
public class TimeLineService {
   @Autowired
    private MongoTemplate mongoTemplate;
   @Async
   public void saveTimeLine (Movement movement){
       Long userId = movement.getUserId();
       List<Friend> friends = mongoTemplate.find(new Query(Criteria.where("userId").is(userId)), Friend.class);
       for (Friend friend : friends) {
           MovementTimeLine movementTimeLine = new MovementTimeLine();
           movementTimeLine.setUserId(movement.getUserId());
           movementTimeLine.setFriendId(friend.getFriendId());
           movementTimeLine.setCreated(movement.getCreated());
           movementTimeLine.setMovementId(movement.getId());
           mongoTemplate.save(movementTimeLine);
       }
   }
}
